import { MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from 'nestjs-config';
import { resolve } from 'path';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { LoggerMiddleware } from './common/middleware/logger.middleware';
import { User } from './model/user.model';
import { CatsModule } from './modules/cats.module';
import { PhotoModule } from './modules/photo.module';
import { UserModule } from './modules/user.module';

@Module({
  imports: [
    // ConfigModule.load(resolve(__dirname, 'config', '**/!(*.d).{ts,js}')),
    // TypeOrmModule.forRootAsync({
    //   useFactory: (config: ConfigService) => config.get('database'),
    //   inject: [ConfigService],
    // }),
    MongooseModule.forRoot('mongodb://root:Lzx0203.@49.234.54.97/zoom'
      //   , {
      //   // useNewUrlParser: true,
      //   // useUnifiedTopology: true,
      //   dbName:'zoom',
      //   auth: {
      //     username: "root",
      //     password: "Lzx0203.",
      //   }
      // }
    ),
    TypeOrmModule.forRoot(
      {
        type: 'mysql',
        host: '49.234.54.97',
        port: 3306,
        username: 'root',
        password: 'Lzx0203.',
        database: 'zoom_db',
        entities: ['/src/model/*.model{.ts,.js}'],
        synchronize: true,
        autoLoadEntities: true,//自动导入entity
      }
    ),
    UserModule,
    PhotoModule,
    CatsModule
  ],
  // controllers: [AppController],
  // providers: [AppService],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(LoggerMiddleware)
      .forRoutes("users");
    // .forRoutes({ path: 'users/get', method: RequestMethod.GET });//特定请求 users/get
  }
}
